<?php

namespace Home\Controller;
use Think\Controller;

//header("Content-type:text/html;charset=utf-8");

class PayController extends Controller{
	//在类初始化方法中，引入相关类库    
	private $Goods;
	private $order;
	private $userAddress;

    public function __construct()
    {
        parent::__construct();
        $this->Goods = D('Goods');
        $this->Order = M('Order_info');
        $this->userAddress = D('UserAddress');
    }
    ///判断是以哪种支付方式提交的 
	public function product_pay(){
		$arr = $_POST;
		if ($arr['pay_type']==0)
        {      // pay_type = 0  货到付款
            echo "货到付款"; exit;
        }
        elseif($arr['pay_type']==1)
        {
            C('LAYOUT_ON', false);
            $data['pay_type'] = $_POST['pay_type'];
            $this->order->where('id=' . $arr['id'])->save($data);
            $this->redirect('weixinpay', array('id' => $arr['id']));
            // echo '这是微信支付通道！';
        }
	}

    public function getcode(){
        include_once('/Data/WxPayPubHelper/WxPayPubHelper.php');
        $jsApi = new JsApi_pub();
        $url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);
        Header("Location:".$url); 
    }

	public function weixinpay($id=''){
      
		 C('LAYOUT_ON',false);
        /**
         * JS_API支付demo
         * ====================================================
         * 在微信浏览器里面打开H5网页中执行JS调起支付。接口输入输出数据格式为JSON。
         * 成功调起支付需要三个步骤：
         * 步骤1：网页授权获取用户openid
         * 步骤2：使用统一支付接口，获取prepay_id
         * 步骤3：使用jsapi调起支付
        */
        // import('Class.WxPayPubHelper',APP_PATH);
        // include_once(APP_PATH."Data/WxPayPubHelper/WxPayPubHelper.php");
        include_once('/Data/WxPayPubHelper/WxPayPubHelper.php');

        //使用jsapi接口
        $jsApi = new JsApi_pub();
    
        //=========步骤1：网页授权获取用户openid============
        //通过code获得openid
        // $code = $_GET['code'];
        if (isset($_GET['code'])) {
            $code = $_GET['code'];
            $jsApi->setCode($code);
            $openid = $jsApi->getOpenId();
        }else{
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=XXXXXaaaaaaaaaaaa&redirect_uri=http://www.rexury.com/HomePhone/pay/weixinpay.html?order_id=".$id."&response_type=code&scope=snsapi_base&state=STATE&#wechat_redirect";
            Header("Location:".$url); 
        }
   
        //=========步骤2：使用统一支付接口，获取prepay_id============
        //使用统一支付接口
        $unifiedOrder = new UnifiedOrder_pub();
        
        //设置统一支付接口参数
        //设置必填参数
        //appid已填,商户无需重复填写
        //mch_id已填,商户无需重复填写
        //noncestr已填,商户无需重复填写
        //spbill_create_ip已填,商户无需重复填写
        // //sign已填,商户无需重复填写
        // $id = $_COOKIE['id'];
        $data = $this->order->where('id='.$_GET['order_id'])->find();//获取网站订单信息
         
        $out_trade_no = $data['order_num'];
        $sum_price = ($data['sum_price'])*100;
        $unifiedOrder->setParameter("openid",$openid);//商品描述
        $unifiedOrder->setParameter("body","流奢网".$out_trade_no);//商品描述
        //自定义订单号，此处仅作举例
        // $timeStamp = time();
        // $out_trade_no = WxPayConf_pub::APPID.$timeStamp;
        
        $unifiedOrder->setParameter("out_trade_no",$out_trade_no);//商户订单号 
        $unifiedOrder->setParameter("total_fee",$sum_price);//总金额
        $unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 
        $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
        //非必填参数，商户可根据实际情况选填
        //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号  
        //$unifiedOrder->setParameter("device_info","XXXX");//设备号 
        //$unifiedOrder->setParameter("attach","XXXX");//附加数据 
        //$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
        //$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间 
        //$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记 
        //$unifiedOrder->setParameter("openid","XXXX");//用户标识
        //$unifiedOrder->setParameter("product_id","XXXX");//商品ID

        $prepay_id = $unifiedOrder->getPrepayId();
        //=========步骤3：使用jsapi调起支付============
        $jsApi->setPrepayId($prepay_id);

        $jsApiParameters = $jsApi->getParameters();
        // p($jsApiParameters);
        // exit();
        //测试是否运行
        // $email ='980502547@qq.com' ;//小杨的QQ邮箱
        // $config = C('THINK_EMAIL');
        // $emailtype = "HTML"; //信件类型，文本:text；网页：HTML
        // $to = $email;
        // $from = $config['FROM_EMAIL'];
        // $emailsubject = "用户实名认证申请";
            
        //     // $url = 'http://www.rexury.com/Admin';
        // $emailbody = $jsApiParameters;
        // think_send_mail($to, $from, $emailsubject, $emailbody, $emailtype);
        // $order = $this->order->where('id='.$_GET['id'])->find();
        $p_id = explode(',', $data['product_id']);
        $num = count($p_id);//订单商品数量
        // p($order);
        // P($num);
        $this->assign('num',$num);
        // $this->assign('order',$order);
        $this->assign('order',$data);
        $this->assign('jsApiParameters',$jsApiParameters);
//        $this->display('payorder');
        // $jsApiParameters
        echo $jsApiParameters;
        echo "<hr>ahhahahha";
        exit;
	}
}


